package ai.zixing.mashibing.new_class.class02;

import ai.zixing.mashibing.new_class.class01.Code07_SelectionSort;

public class CompareTest {
    public static void main(String[] args) {
        int maxLen = 50;
        int maxValue = 1000;
        int testTimes = 10000;
        for (int i = 0; i < testTimes; i++) {
            int[] arr1 = randomArr(maxLen, maxValue);
            // arr2 可以找出错误的例子
            int[] arr2 = copyArray(arr1);
            Code07_SelectionSort.selectSort(arr2);
            if (!isSorted(arr1)) {
                System.out.println("排序错误");
            }
        }
    }


    // 返回一个数组arr，arr长度[0,maxLen-1],arr中的每个值[0,maxValue-1]
    public static int[] randomArr(int maxLen, int maxValue) {
        int len = (int)Math.random() * maxLen;
        int[] ans = new int[len];
        for (int i = 0; i < len; i++) {
            ans[i] = (int) (Math.random() * maxValue);
        }
        return ans;
    }

    public static int[] copyArray(int[] arr) {
        int[] ans = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ans[i] = arr[i];
        }
        return ans;
    }

    // 判断数组是否有序
    public static boolean isSorted(int[] arr) {
        if (arr.length < 2) {
            return true;
        }
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (max > arr[i]) {
                return false;
            }
            max = Math.max(max, arr[i]);
        }
        return true;
    }
}

